package pers.zh.parking.controller;

import com.github.pagehelper.util.StringUtil;
import com.sun.xml.internal.ws.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import pers.zh.parking.common.BaseController;
import pers.zh.parking.common.Response;
import pers.zh.parking.po.Car;
import pers.zh.parking.po.ParkingLot;
import pers.zh.parking.po.ParkingSpace;
import pers.zh.parking.service.ILotService;
import pers.zh.parking.service.ISpaceService;

import java.util.List;
import java.util.function.Consumer;

@RestController
@RequestMapping("space")
public class SpaceController extends BaseController {

    @Autowired
    ISpaceService spaceService;
    @Autowired
    ILotService lotService;

    /**
     * 停车场车位造数据,批量
     *
     * @return
     */
    @GetMapping("generate")
    public Response generate() {
        List<ParkingLot> lots = lotService.getAllLots();
        if (spaceService.batchGenerate(lots)) {
            return Response.ok("车位数据生成成功");
        }
        return Response.fail_500("车位数据生成失败");
    }

    /**
     * 根据停车场id查询车位列表
     */
    @GetMapping("list/{lotId}")
    public Response list(@PathVariable int lotId) {
        if (lotId == 0) {
            return Response.fail_400("参数错误：0");
        }
        return Response.ok(spaceService.getSpacesByLotId(lotId), "查询成功");
    }

    /**
     * 新增车位信息
     * @param space
     * @return
     */
    @PostMapping("add")
    public Response add(@RequestBody ParkingSpace space) {
        if (ObjectUtils.isEmpty(space.getLotId())) {
            return Response.fail_400("参数错误：0");
        }
        if (spaceService.add(space)) {
            return Response.ok("新增成功");
        }
        return Response.fail_500("新增失败");
    }

    /**
     * 删除车位
     * @param list
     * @return
     */
    @PostMapping("delete")
    public Response delete(@RequestBody List<ParkingSpace> list) {
        if (ObjectUtils.isEmpty(list)) {
            return Response.fail_400("要删除的数据不能为空");
        }
        for (ParkingSpace p : list) {
            if (!"1".equals(p.getStatus())) {
                return Response.fail_400("只能删除空闲状态的车位");
            }
        }
        if (spaceService.batchDel(list)) {
            return Response.ok("删除成功");
        } else {
            return Response.fail_500("删除失败");
        }
    }

    /**
     * 修改车位信息
     * @param space
     * @return
     */
    @PostMapping("update")
    public Response update(@RequestBody ParkingSpace space) {
        if (ObjectUtils.isEmpty(space.getSpaceId())) {
            return Response.fail_400("参数错误：0");
        }
        if (spaceService.update(space)) {
            return Response.ok("修改成功");
        }
        return Response.fail_500("修改失败");
    }

}
